// ============================================================================
// 数据库相关类型定义
// ============================================================================

import { TaskStatus, TaskType, LogLevel } from './index'

/** 数据库表基础接口 */
export interface BaseEntity {
  id: string
  createdAt: Date
  updatedAt: Date
}

/** 账号表 */
export interface AccountEntity extends BaseEntity {
  uid: string
  nickname: string
  avatar: string
  isVip: boolean
  cookies: string // JSON string
  isValid: boolean
  lastValidated?: Date
  loginMethod?: string
  followersCount?: number
  followingCount?: number
  statusesCount?: number
  description?: string
  location?: string
  verifiedType?: number
  verifiedReason?: string
}

/** 任务表 */
export interface TaskEntity extends BaseEntity {
  type: TaskType
  params: string // JSON string
  status: TaskStatus
  accountId: string
  startedAt?: Date
  completedAt?: Date
  progress?: string // JSON string
  result?: string // JSON string
  priority?: number
  retryCount?: number
  maxRetries?: number
  tags?: string // JSON array string
}

/** 任务日志表 */
export interface TaskLogEntity extends BaseEntity {
  taskId: string
  level: LogLevel
  message: string
  data?: string // JSON string
  source?: string
}

/** 微博数据表 */
export interface WeiboEntity extends BaseEntity {
  weiboId: string
  text: string
  userId: string
  userNickname: string
  userAvatar: string
  source?: string
  repostsCount?: number
  commentsCount?: number
  likesCount?: number
  images?: string // JSON array string
  video?: string // JSON string
  location?: string // JSON string
  isRepost?: boolean
  originalWeiboId?: string
  tags?: string // JSON array string
  taskId: string
}

/** 用户数据表 */
export interface UserEntity extends BaseEntity {
  uid: string
  nickname: string
  avatar: string
  isVip: boolean
  followersCount?: number
  followingCount?: number
  statusesCount?: number
  description?: string
  location?: string
  verifiedType?: number
  verifiedReason?: string
  lastUpdated?: Date
}

/** 配置表 */
export interface ConfigEntity extends BaseEntity {
  key: string
  value: string // JSON string
  description?: string
  isSystem?: boolean
}

/** 数据库连接配置 */
export interface DatabaseConfig {
  filename: string
  options?: {
    verbose?: (message?: any, ...additionalArgs: any[]) => void
    fileMustExist?: boolean
    timeout?: number
    readonly?: boolean
  }
}

/** 数据库迁移 */
export interface Migration {
  version: number
  name: string
  up: (db: any) => Promise<void>
  down: (db: any) => Promise<void>
}

/** 查询选项 */
export interface QueryOptions {
  limit?: number
  offset?: number
  orderBy?: string
  orderDirection?: 'ASC' | 'DESC'
  where?: Record<string, any>
}

/** 数据库统计 */
export interface DatabaseStats {
  totalAccounts: number
  validAccounts: number
  totalTasks: number
  completedTasks: number
  totalWeibos: number
  totalUsers: number
  databaseSize: number
  lastBackup?: Date
}

/** 备份配置 */
export interface BackupConfig {
  enabled: boolean
  interval: number // 小时
  maxBackups: number
  backupPath: string
  compression: boolean
}

/** 数据清理配置 */
export interface CleanupConfig {
  enabled: boolean
  retentionDays: number
  cleanupInterval: number // 小时
  cleanupLogs: boolean
  cleanupFailedTasks: boolean
  cleanupOldBackups: boolean
}
